{extend name="common/layout" /}
{block name="title"}域名管理{/block}
{block name="main"}
<div class="modal" id="modal-store" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content animated flipInX">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span
							aria-hidden="true">&times;</span><span
							class="sr-only">Close</span></button>
				<h4 class="modal-title" id="modal-title">添加域名</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" id="form-store">
					<input type="hidden" name="recordcount"/>
					<div class="form-group">
						<label class="col-sm-3 control-label">域名账户</label>
						<div class="col-sm-9">
							<select name="aid" class="form-control">
								{foreach $accounts as $k=>$v}
									<option value="{$k}">{$v}</option>
								{/foreach}
							</select>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label">选择域名</label>
						<div class="col-sm-9">
							<select name="domain" id="domainList" class="form-control"></select>
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" id="store" onclick="saveAdd()">添加</button>
			</div>
		</div>
	</div>
</div>
<div class="modal" id="modal-store2" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog">
		<div class="modal-content animated flipInX">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span
							aria-hidden="true">&times;</span><span
							class="sr-only">Close</span></button>
				<h4 class="modal-title" id="modal-title">修改域名配置</h4>
			</div>
			<div class="modal-body">
				<form class="form-horizontal" id="form-store2">
					<input type="hidden" name="id"/>
					<div class="form-group">
						<label class="col-sm-3 control-label">是否隐藏</label>
						<div class="col-sm-9">
							<div class="input-group">
								<select name="is_hide" class="form-control">
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
								<a tabindex="0" class="input-group-addon" role="button" data-toggle="popover" data-trigger="focus" title="" data-placement="bottom" data-content="隐藏后仅管理员可见，普通用户在列表不可见" data-original-title="说明"><span class="glyphicon glyphicon-info-sign"></span></a>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label">对接开关</label>
						<div class="col-sm-9">
							<div class="input-group">
								<select name="is_sso" class="form-control">
									<option value="0">否</option>
									<option value="1">是</option>
								</select>
								<a tabindex="0" class="input-group-addon" role="button" data-toggle="popover" data-trigger="focus" title="" data-placement="bottom" data-content="对接开关开启后，将允许通过API接口一键登录该域名独立管理面板" data-original-title="说明"><span class="glyphicon glyphicon-info-sign"></span></a>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-sm-3 control-label no-padding-right">备注</label>
						<div class="col-sm-9">
							<input type="text" class="form-control" name="remark" placeholder="">
						</div>
					</div>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
				<button type="button" class="btn btn-primary" id="store" onclick="saveEdit()">保存</button>
			</div>
		</div>
	</div>
</div>
<div class="row">
	<div class="col-xs-12 center-block" style="float: none;">
	<div class="panel panel-default panel-intro">
	<div class="panel-body">
	
<form onsubmit="return searchSubmit()" method="GET" class="form-inline" id="searchToolbar">
  <div class="form-group">
	<label>搜索</label>
    <input type="text" class="form-control" name="kw" placeholder="域名或备注">
  </div>
  <div class="form-group">
	<select name="type" class="form-control"><option value="">所有平台</option>{foreach $types as $k=>$v}
		<option value="{$k}">{$v}</option>
	{/foreach}</select>
  </div>
  <button type="submit" class="btn btn-primary"><i class="fa fa-search"></i> 搜索</button>
  <a href="javascript:searchClear()" class="btn btn-default" title="刷新域名列表"><i class="fa fa-refresh"></i> 刷新</a>
  {if request()->user['level'] eq 2}<a href="javascript:addframe()" class="btn btn-success"><i class="fa fa-plus"></i> 添加</a>{/if}
</form>

      <table id="listTable">
	  </table>
    </div>
  </div>
</div>
</div>
{/block}
{block name="script"}
<script src="{$cdnpublic}layer/3.1.1/layer.js"></script>
<script src="{$cdnpublic}bootstrap-table/1.21.4/bootstrap-table.min.js"></script>
<script src="{$cdnpublic}bootstrap-table/1.21.4/extensions/page-jump-to/bootstrap-table-page-jump-to.min.js"></script>
<script src="{$cdnpublic}select2/4.0.13/js/select2.min.js"></script>
<script src="{$cdnpublic}select2/4.0.13/js/i18n/zh-CN.min.js"></script>
<script src="/static/js/custom.js"></script>
<script>
var userLevel = "{:request()->user['level']}";
$(document).ready(function(){
	updateToolbar();
	const defaultPageSize = 15;
	const pageNumber = typeof window.$_GET['pageNumber'] != 'undefined' ? parseInt(window.$_GET['pageNumber']) : 1;
	const pageSize = typeof window.$_GET['pageSize'] != 'undefined' ? parseInt(window.$_GET['pageSize']) : defaultPageSize;

	$("#listTable").bootstrapTable({
		url: '/domain/data',
		pageNumber: pageNumber,
		pageSize: pageSize,
		classes: 'table table-striped table-hover table-bordered',
		columns: [
			{
				field: 'id',
				title: 'ID'
			},
			{
				field: 'typename',
				title: '平台账户',
				formatter: function(value, row, index) {
					return '<span title="'+row.aremark+'" data-toggle="tooltip" data-placement="right" title="Tooltip on right"><img src="/static/images/'+row.type+'.ico" class="type-logo"></img>'+value+'('+row.aid+')</span>';
				}
			},
            {
				field: 'name',
				title: '域名',
				formatter: function(value, row, index) {
					return '<a href="/record/'+row.id+'" title="进入解析记录管理" onclick="loading()">'+value+'</a>';
				}
			},
			{
				field: 'recordcount',
				title: '记录数'
			},
			{
				field: 'addtime',
				title: '添加时间'
			},
			{
				field: 'remark',
				title: '备注'
			},
			{
				field: 'is_hide',
				title: '是否隐藏',
				formatter: function(value, row, index) {
					return value==1?'<font color="grey">是</font>':'<font color="blue">否</font>';
				}
			},
			{
				field: 'is_sso',
				title: '对接开关',
				formatter: function(value, row, index) {
					return value==1?'<font color="green">是</font>':'<font color="red">否</font>';
				}
			},
			{
				field: '',
				title: '操作',
				formatter: function(value, row, index) {
					var html = '<a href="/record/'+row.id+'" class="btn btn-success btn-xs" onclick="loading()">解析</a>';
					if(userLevel == '2'){
						html += ' <a href="javascript:editframe('+row.id+')" class="btn btn-info btn-xs">配置</a>';
						html += ' <a href="javascript:delItem('+row.id+')" class="btn btn-danger btn-xs">删除</a>';
					}
					return html;
				}
			},
		],
		onLoadSuccess: function(data) {
			$('[data-toggle="tooltip"]').tooltip()
		}
	})

	$("#form-store select[name=aid]").change(function(){
		getDomainList()
	})

	$('#domainList').on('select2:select', function (e) {
		var data = e.params.data;
		$("#form-store input[name=recordcount]").val(data.recordcount);
	});

	$('[data-toggle="popover"]').popover()
})
function addframe(){
	$("#modal-store").modal('show');
	var aid = $("#form-store select[name=aid]").val();
	if(aid != ''){
		getDomainList()
	}
}
function saveAdd(){
	var aid = $("#form-store select[name=aid]").val();
	var select = $('#domainList').select2('data');
	if(select.length == 0){
		layer.alert('请选择域名！');return false;
	}
	var name = select[0].text;
	var thirdid = select[0].id;
	var recordcount = $("#form-store input[name=recordcount]").val();
	if(aid=='' || thirdid==''){
		layer.alert('请确保各项不能为空！');return false;
	}
	var ii = layer.load(2);
	$.ajax({
		type : 'POST',
		url : '/domain/op/act/add',
		data : {aid: aid, thirdid: thirdid, name: name, recordcount: recordcount},
		dataType : 'json',
		success : function(data) {
			layer.close(ii);
			if(data.code == 0){
				layer.alert(data.msg,{
					icon: 1,
					closeBtn: false
				}, function(){
					layer.closeAll();
					$("#modal-store").modal('hide');
					searchSubmit();
				});
			}else{
				layer.alert(data.msg, {icon: 2})
			}
		}
	});
}
function editframe(id){
	var ii = layer.load(2);
	$.ajax({
		type : 'POST',
		url : '/domain/op/act/get',
		data : {id: id},
		dataType : 'json',
		success : function(data) {
			layer.close(ii);
			if(data.code == 0){
				$("#modal-store2").modal('show');
				$("#form-store2 input[name=id]").val(data.data.id);
				$("#form-store2 select[name=is_hide]").val(data.data.is_hide);
				$("#form-store2 select[name=is_sso]").val(data.data.is_sso);
				$("#form-store2 input[name=remark]").val(data.data.remark);
			}else{
				layer.alert(data.msg, {icon: 2})
			}
		}
	});
}
function saveEdit(){
	var ii = layer.load(2);
	$.ajax({
		type : 'POST',
		url : '/domain/op/act/edit',
		data : $("#form-store2").serialize(),
		dataType : 'json',
		success : function(data) {
			layer.close(ii);
			if(data.code == 0){
				layer.alert(data.msg,{
					icon: 1,
					closeBtn: false
				}, function(){
					layer.closeAll();
					$("#modal-store2").modal('hide');
					searchSubmit();
				});
			}else{
				layer.alert(data.msg, {icon: 2})
			}
		}
	});
}
function delItem(id) {
	var confirmobj = layer.confirm('确定要删除此域名吗？删除域名不会影响已添加的解析', {
	  btn: ['确定','取消']
	}, function(){
	  var ii = layer.load(2);
	  $.ajax({
		type : 'POST',
		url : '/domain/op/act/del',
		data : {id: id},
		dataType : 'json',
		success : function(data) {
			layer.close(ii);
			if(data.code == 0){
				layer.closeAll();
				searchSubmit();
			}else{
				layer.alert(data.msg, {icon: 2});
			}
		}
	  });
	}, function(){
		layer.close(confirmobj);
	});
}
function getDomainList(){
	$('#domainList').empty();
	$('#domainList').select2({
		placeholder: '请选择域名',
		language: "zh-CN",
		ajax:{
			url: '/domain/list',
			type: "post",
			dataType: 'json',
			delay: 300,
			data: function(params) {
				var aid = $("#form-store select[name=aid]").val();
				var data = {
					aid: aid,
					kw: params.term,
					page: params.page || 1,
					pagesize: 10
				};
				return data;
			},
			processResults: function (data, params) {
				params.page = params.page || 1
				if(data.code == 0){
					var resultData = [];
					for (var i = 0; i < data.data.list.length; i++) {
						resultData.push({'id': data.data.list[i].DomainId, 'text': data.data.list[i].Domain, 'recordcount': data.data.list[i].RecordCount});
					}
					return {
						results: resultData,
						pagination: {
							more: data.data.total > params.page * 10
						}
					};
				}else{
					layer.alert(data.msg, {icon: 2});
					return {
		                results: []
		            };
				}
			}
		},
		cache:false
	});
}
function loading(){
	layer.load(2);
}
document.addEventListener("visibilitychange", function() {
	if (document.visibilityState === "visible") {
		layer.closeAll();
	}
});
</script>
{/block}